package com.example.controller;

import com.example.dao.UserDao;
import com.example.entity.User;
import com.example.entity.Users;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author 何星霖
 * @date 2018/10/15 9:46
 */
@RestController
@RequestMapping(value = "/user")
@Api(description="用户操作接口")
public class UserContronller {
    @Autowired
    private UserService userService;
    @Autowired
    private UserDao userDao;

    @ApiOperation(value = "新增用户", notes = "新增用户")
    @RequestMapping(value = "/insertUser",method = RequestMethod.GET)
    public  Integer insertUser(@RequestParam("name") String name,@RequestParam("age") Integer age){
        System.out.println("name="+name+"age="+age);
        int a=userService.insert(name,age);
        System.out.println("---aaaaa---"+a);
        return a;
    }
    @ApiOperation(value = "查找用户", notes = "查找用户")
    @RequestMapping(value = "/findByName",method = RequestMethod.POST)
    public List<User> findByName(@RequestBody String name){
        List<User> userList=userService.findByName(name);
        return userList;
    }
    @ApiOperation(value = "查找用户分页", notes = "查找用户分页")
    @RequestMapping(value = "/findUserList" ,method = RequestMethod.POST)
    public PageInfo<User> findUserList( @RequestParam(value = "page")  int page,
                                        @RequestParam(value = "size") int size ){
        return userService.findUserList(page, size);
    }
    @ApiOperation(value = "JPA查找用户", notes = "JPA查找用户")
    @RequestMapping(value = "/findUserByJpa" ,method = RequestMethod.GET)
    public Optional<Users> findUserByJpa(@RequestParam(value = "id")  Integer id){
        Optional<Users> user= userDao.findById(id);
        System.out.println("user.name="+user.toString());
        return user;
    }
    @ApiOperation(value = "cache缓存", notes = "cache缓存")
    @RequestMapping(value = "/findUserByname" ,method = RequestMethod.GET)
    public Users findUserByname(@RequestParam(value = "name")  String name){
        Users user= userDao.findByName(name);
        System.out.println("user.name="+user.toString());
        return user;
    }
}
